package com.sinosoft.perfect.system._authorization.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.sinosoft.perfect.system._.po.TUserRolesysRelation;
import com.sinosoft.perfect.system._.util.Producer;
import com.sinosoft.perfect.system._authorization.dao.IUserRolesysAuthorizeDao;

public class UserRolesysAuthorizeImpl implements IUserRolesysAuthorizeService {

	private IUserRolesysAuthorizeDao iuserRolesysAuthorizeDao;

	public void setIuserRolesysAuthorizeDao(IUserRolesysAuthorizeDao iuserRolesysAuthorizeDao) {
		this.iuserRolesysAuthorizeDao = iuserRolesysAuthorizeDao;
	}

	@Override
	public boolean delAllRoleUsersOfUnitAndAddUsersRoleOfUnit(String unitId, String chooseRoleSystemsString) {
		boolean boolResult = true;
		String[] chooseRoleSystems = this.getChooseRoleSystemsFromString(chooseRoleSystemsString);
		List<String> userIds = null;
		List<TUserRolesysRelation> tuserRolesysRelationByUnitResultList = null;

		if (chooseRoleSystems != null && chooseRoleSystems.length > 0) {
			try {
				userIds = iuserRolesysAuthorizeDao.queryUserIdsOfUnit(unitId);
			} catch (SQLException e) {
				boolResult = false;
				e.printStackTrace();
			}
			if (userIds != null) {
				for (int i = 0; i < userIds.size(); i++) {
					String userId = userIds.get(i);
					List<TUserRolesysRelation> tuserRolesysRelationResultList = this.getTUserRolesysRelationsByUserAndchooseRoleSystems(userId,
							chooseRoleSystems);
					if (tuserRolesysRelationResultList != null) {
						if (tuserRolesysRelationByUnitResultList == null)
							tuserRolesysRelationByUnitResultList = new ArrayList<TUserRolesysRelation>();
						tuserRolesysRelationByUnitResultList.addAll(tuserRolesysRelationResultList);
					}
				}
			}
		}

		try {
			iuserRolesysAuthorizeDao.delAllRoleUsersOfUnitAndAddUsersRoleOfUnit(unitId, tuserRolesysRelationByUnitResultList);
		} catch (SQLException e) {
			boolResult = false;
			e.printStackTrace();
		}
		return boolResult;
	}

	@Override
	public boolean delAllRolesOfUserAndAddUserRoles(String userId, String chooseRoleSystemsString) {
		boolean boolResult = true;
		String[] chooseRoleSystems = this.getChooseRoleSystemsFromString(chooseRoleSystemsString);
		List<TUserRolesysRelation> tuserRolesysRelationResultList = null;
		if (chooseRoleSystems != null && chooseRoleSystems.length > 0) {
			tuserRolesysRelationResultList = this.getTUserRolesysRelationsByUserAndchooseRoleSystems(userId, chooseRoleSystems);
		}

		try {
			iuserRolesysAuthorizeDao.delAllRolesOfUserAndAddUserRoles(userId, tuserRolesysRelationResultList);
		} catch (SQLException e) {
			boolResult = false;
			e.printStackTrace();
		}
		return boolResult;
	}

	private String[] getChooseRoleSystemsFromString(String chooseRoleSystemsString) {
		if (chooseRoleSystemsString != null) {
			return chooseRoleSystemsString.split(",");
		}
		return null;
	}

	private List<TUserRolesysRelation> getTUserRolesysRelationsByUserAndchooseRoleSystems(String userId, String[] chooseRoleSystems) {
		List<TUserRolesysRelation> tuserRolesysRelationResultList = null;
		for (int i = 0; i < chooseRoleSystems.length; i++) {
			if (tuserRolesysRelationResultList == null)
				tuserRolesysRelationResultList = new ArrayList<TUserRolesysRelation>();
			String chooseRoleSystemString = chooseRoleSystems[i];
			TUserRolesysRelation tuserRolesysRelation = this.getNewTUserRolesysRelation(userId, chooseRoleSystemString);
			tuserRolesysRelationResultList.add(tuserRolesysRelation);
		}
		return tuserRolesysRelationResultList;
	}

	private TUserRolesysRelation getNewTUserRolesysRelation(String userId, String roleSystemId) {
		TUserRolesysRelation tuserRolesysRelation = new TUserRolesysRelation();
		String manageId = Producer.generateIdOfTen();
		tuserRolesysRelation.setManageId(manageId);
		tuserRolesysRelation.setRoleSystemId(roleSystemId);
		tuserRolesysRelation.setUserMainId(userId);
		return tuserRolesysRelation;
	}

	@Override
	public List<TUserRolesysRelation> queryRolesystemsOfUser(String userId) throws SQLException {
		return iuserRolesysAuthorizeDao.queryRolesystemsOfUser(userId);
	}

	@Override
	public boolean deleteAndinsertAllUsersInOneRoleSystem(String addUserIds,String delUserIds, String roleSystemId) throws SQLException {
		boolean flag = false;
		if (delUserIds != null && !"".equals(delUserIds)) {
			String delUserIdArray[] = delUserIds.split(",");
			for (int i = 0; i < delUserIdArray.length; i++) {
				TUserRolesysRelation tUserRolesysRelation = new TUserRolesysRelation();
				tUserRolesysRelation.setRoleSystemId(roleSystemId);
				tUserRolesysRelation.setUserMainId(delUserIdArray[i]);
				iuserRolesysAuthorizeDao.deleteTUserRolesysRelation(tUserRolesysRelation);
				flag = true;
			}
		}
		if (addUserIds != null && !"".equals(addUserIds)) {
			String addUserIdArray[] = addUserIds.split(",");
			for (int i = 0; i < addUserIdArray.length; i++) {
				TUserRolesysRelation tuserRolesysRelation = new TUserRolesysRelation();
				tuserRolesysRelation.setManageId(Producer.generateIdOfTen());
				tuserRolesysRelation.setRoleSystemId(roleSystemId);
				tuserRolesysRelation.setUserMainId(addUserIdArray[i]);
				iuserRolesysAuthorizeDao.insertByTUserRolesysRelation(tuserRolesysRelation);
				flag = true;
			}
		}
		return flag;
	}

}
